package com.swiot.quartz.domain;

import com.alibaba.fastjson2.JSONObject;
import com.swiot.biz.constants.TemplateConstants;
import com.swiot.biz.domain.TemplateData;
import com.swiot.common.utils.StringUtils;

public class RuleMessage {

    private TemplateData templateData;
    private String fieldName;
    private String value;
    private String ruleKey;
    private String ruleId;
    private String ruleParam;
    private String ruleFunc;

    public TemplateData getTemplateData() {
        return templateData;
    }

    public void setTemplateDataId(TemplateData templateData) {
        this.templateData = templateData;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getRuleKey() {
        return ruleKey;
    }

    public void setRuleKey(String ruleKey) {
        this.ruleKey = ruleKey;
    }

    public String getRuleId() {
        return ruleId;
    }

    public void setRuleId(String ruleId) {
        this.ruleId = ruleId;
    }

    public String getRuleParam() {
        return ruleParam;
    }

    public void setRuleParam(String ruleParam) {
        this.ruleParam = ruleParam;
    }

    public String getRuleFunc() {
        return ruleFunc;
    }

    public void setRuleFunc(String ruleFunc) {
        this.ruleFunc = ruleFunc;
    }

    public static RuleMessage builder(TemplateData templateData, String fieldName, JSONObject fieldValue, JSONObject ruleCache) {

        if (fieldValue == null || ruleCache == null) {
            return null;
        }
        if (fieldValue.isEmpty() || !fieldValue.containsKey("value") || !fieldValue.containsKey("alarm")) {
            return null;
        }
        RuleMessage ruleMessage = new RuleMessage();
        ruleMessage.fieldName = fieldName;
        ruleMessage.templateData = templateData;
        ruleMessage.value = fieldValue.getString("value");
        ruleMessage.ruleKey = fieldValue.getString("alarm");
        JSONObject ruleKeyCache = ruleCache.getJSONObject(ruleMessage.ruleKey);
        ruleMessage.ruleFunc = ruleKeyCache.getString(TemplateConstants.AlarmRules.RULE_FUNC);
        ruleMessage.ruleId = ruleKeyCache.getString("id");
        ruleMessage.ruleParam = ruleKeyCache.getString(TemplateConstants.AlarmRules.RULE_PARAM);

        return ruleMessage;
    }

    /**
     * 获取告警方法
     *
     * @param ruleCache
     * @return
     */
    private String getAlarmMethod(JSONObject ruleCache) {
        String result = "defaultAlarm";
        String alarmMethod = ruleCache.getString(TemplateConstants.AlarmRules.RULE_FUNC);
        if (StringUtils.isNotBlank(alarmMethod)) {
            result = alarmMethod;
        }
        return result;
    }

    @Override
    public String toString() {
        return "RuleMessage{" +
                "templateData=" + ((templateData == null) ? null : templateData.getId()) +
                ", fieldName='" + fieldName + '\'' +
                ", value='" + value + '\'' +
                ", ruleKey='" + ruleKey + '\'' +
                ", ruleId='" + ruleId + '\'' +
                ", ruleParam='" + ruleParam + '\'' +
                ", ruleFunc='" + ruleFunc + '\'' +
                '}';
    }
}
